//
//  MineHeaderView.m
//  FD
//
//  Created by Yasin on 2018/10/10.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "MineHeaderView.h"
#import "Masonry.h"
#import "MineHeaderBottomTitleCountView.h"
@interface MineHeaderView()

//第一部分 6个控件
@property (nonatomic, strong) UIView *div1;
/**左设置按钮*/
@property (nonatomic, strong) UIButton *settingBtn;//btn
/**12-3测试说不好点,放一个超级大的透明的按钮*/
@property (nonatomic, strong) UIButton *settingBigTransparentBtn;

/**右边的铃铛消息按钮*/
@property (nonatomic, strong) UIButton *msgBtn;//btn
/**超大透明按钮*/
@property (nonatomic, strong) UIButton *msgBigTransparentBtn;
/**redDot*/
@property (nonatomic, strong) UIView *redDotView;


@property (nonatomic, strong) UIImageView *bgIv;//背景图片iv

/**头像和手机号码放在一个div中*/
@property (nonatomic, strong) UIView *iconAndPhontLbDiv;
@property (nonatomic, strong) UIImageView *iconIv;//头像iv
@property (nonatomic, strong) UILabel *phoneLb;//手机号lb


////第二部分
//@property (nonatomic, strong) UIView *div2;
//

/**下面的view*/
@property (nonatomic,  strong) UIView *div2;
/**积分*/
@property (nonatomic,  strong) MineHeaderBottomTitleCountView *jiFenView;
/**积分 按钮*/
@property (nonatomic,  strong) UIButton *jiFenBtn;

/**卡券*/
@property (nonatomic,  strong) MineHeaderBottomTitleCountView *kaQuanView;
/**卡券 按钮*/
@property (nonatomic,  strong) UIButton *kaQuanBtn;

/**收藏*/
//@property (nonatomic,  strong) MineHeaderBottomTitleCountView *shouCangView;
/**收藏 按钮*/
//@property (nonatomic,  strong) UIButton *shouCangBtn;

/**线1*/
@property (nonatomic,  strong) UIView *lineView1;
/**线2*/
//@property (nonatomic,  strong) UIView *lineView2;

@end

@implementation MineHeaderView

#pragma mark - 一 init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    
    [self addSubview:self.div1];
    [self.div1 addSubview:self.bgIv];
    [self.div1 addSubview:self.settingBtn];
    [self.div1 addSubview:self.msgBtn];
    [self.msgBtn addSubview:self.redDotView]; //圆点
    self.redDotView.hidden  = YES; //默认隐藏
    [self.div1 addSubview:self.settingBigTransparentBtn]; //用一个大按钮盖住
    [self.div1 addSubview:self.msgBigTransparentBtn]; //用一个大按钮盖住
    
    [self.div1 addSubview:self.iconAndPhontLbDiv];
    [self.iconAndPhontLbDiv addSubview:self.iconIv];
    [self.iconAndPhontLbDiv addSubview:self.phoneLb];
    
    //下半部分
    {
        [self addSubview:self.div2];
        [self.div2 addSubview:self.jiFenView];
        [self.div2 addSubview:self.kaQuanView];
//        [self.div2 addSubview:self.shouCangView];
        [self.div2 addSubview:self.lineView1];
//        [self.div2 addSubview:self.lineView2];
        
        //点击事件：最后添加三个透明个按钮
        [self.div2 addSubview:self.jiFenBtn];
        [self.div2 addSubview:self.kaQuanBtn];
//        [self.div2 addSubview:self.shouCangBtn];
    }
}

#pragma mark - 二 布局
- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self upLayout];
    [self downLayout];
}

//上半部分
- (void)upLayout {
    [self.div1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(180);
    }];
    [self.bgIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.div1);
    }];
    //左设置按钮
    [self.settingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div1).offset(15);
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.top.equalTo(self.div1).offset(20);
    }];
    
    
    //大按钮
    [self.settingBigTransparentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.div1);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    
    //右边消息提示(铃铛)按钮
    [self.msgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.div1).offset(15);
        make.top.equalTo(self.settingBtn.mas_top);
        make.right.equalTo(self.div1).offset(-15);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    [self.redDotView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.msgBtn.mas_top);
        make.right.equalTo(self.msgBtn.mas_right);
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    
    
    //大按钮
    [self.msgBigTransparentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.equalTo(self.div1);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    
    
    //头像 和 手机号 放在同一div中
    [self.iconAndPhontLbDiv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.div1);
        make.bottom.equalTo(self.div1.mas_bottom).offset(- 40);
        make.height.mas_equalTo(60);
    }];
    
    
    //头像
    [self.iconIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconAndPhontLbDiv.mas_left).offset(15);
        make.centerY.equalTo(self.iconAndPhontLbDiv.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    
    //手机号
    [self.phoneLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconIv.mas_right).offset(15);
        make.centerY.equalTo(self.iconAndPhontLbDiv.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(150, 30));
    }];
}

//!!!!: 下半部分 ：h : 80
- (void)downLayout {
    [self.div2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.div1.mas_bottom);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(80);
    }];
    
    //
    CGFloat w = (SCREEN_WIDTH - 6) / 2.0;
    [self.jiFenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div2.mas_left).offset(0);
        make.centerY.equalTo(self.div2.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(w, 80));
    }];
    [self.jiFenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div2.mas_left).offset(0);
        make.centerY.equalTo(self.div2.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(w, 80));
    }];
    
    [self.lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.jiFenView.mas_right);
        make.centerY.equalTo(self.div2.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(2, 40));
    }];
    
    
    [self.kaQuanView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lineView1.mas_right);
        make.centerY.equalTo(self.div2.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(w, 80));
    }];
    [self.kaQuanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lineView1.mas_right);
        make.centerY.equalTo(self.div2.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(w, 80));
    }];
    
//    [self.lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.kaQuanView.mas_right);
//        make.centerY.equalTo(self.div2.mas_centerY);
//        make.size.mas_equalTo(CGSizeMake(1, 40));
//    }];
//
//    [self.shouCangView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.lineView2.mas_right);
//        make.centerY.equalTo(self.div2.mas_centerY);
//        make.size.mas_equalTo(CGSizeMake(w, 80));
//    }];
//    [self.shouCangBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.lineView2.mas_right);
//        make.centerY.equalTo(self.div2.mas_centerY);
//        make.size.mas_equalTo(CGSizeMake(w, 80));
//    }];
}


#pragma mark - 二 getter
- (UIView *)div1 {
    if (!_div1) {
        _div1 = [UIView new];
    }
    return _div1;
}

- (UIButton *)settingBigTransparentBtn {
    if (!_settingBigTransparentBtn) {
        
        _settingBigTransparentBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
//        _settingBigTransparentBtn.backgroundColor = [UIColor redColor];
        [_settingBigTransparentBtn addTarget:self action:@selector(clickSettingBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _settingBigTransparentBtn;
}

- (UIButton *)settingBtn {
    if (!_settingBtn) {
        _settingBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        UIImage *img = [UIImage imageNamed:@"设置"];
        [_settingBtn setImage:img forState:(UIControlStateNormal)];
    }
    return _settingBtn;
}

-(UIButton *)msgBtn {
    if (!_msgBtn) {
        _msgBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_msgBtn setImage:[UIImage imageNamed:@"铃铛"] forState:(UIControlStateNormal)];
    }
    return _msgBtn;
}
- (UIButton *)msgBigTransparentBtn {
    if (!_msgBigTransparentBtn) {
        _msgBigTransparentBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_msgBigTransparentBtn addTarget:self action:@selector(clickMsgBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _msgBigTransparentBtn;
}

- (UIView *)redDotView {
    if (!_redDotView) {
        _redDotView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        _redDotView.backgroundColor = [UIColor redColor];
        _redDotView.layer.cornerRadius = 5;
        _redDotView.layer.masksToBounds = YES;
    }
    return _redDotView;
}


- (UIImageView *)bgIv {
    if (!_bgIv) {
        _bgIv = [[UIImageView alloc] init];
        //黑色背景背景图片
        _bgIv.image = [UIImage imageNamed:@"bg.png"];
    }
    return _bgIv;
}

//头像和手机放在同一个div中
- (UIView *)iconAndPhontLbDiv {
    if (!_iconAndPhontLbDiv) {
        _iconAndPhontLbDiv = [UIView new];
        //添加点击事件
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTapGes)];
        [_iconAndPhontLbDiv addGestureRecognizer:tapGes];
    }
    return _iconAndPhontLbDiv;
}

- (UIImageView *)iconIv {
    if (!_iconIv) {
        //默认头像
        _iconIv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"默认头像.png"]];
        //切圆角
        _iconIv.backgroundColor = [UIColor whiteColor];
        _iconIv.layer.masksToBounds = YES;
        _iconIv.layer.cornerRadius = 25;
        _iconIv.userInteractionEnabled = YES; //打开交互
    }
    return _iconIv;
}
- (UILabel *)phoneLb {
    if (!_phoneLb) {
        _phoneLb = [[UILabel alloc] init];
        _phoneLb.textColor = [UIColor whiteColor];
        _phoneLb.userInteractionEnabled = YES; //打开交互
    }
    return _phoneLb;
}

//!!!!:下半部分
- (UIView *)div2 {
    if (!_div2) {
        _div2 = [UIView new];
        _div2.backgroundColor = [UIColor whiteColor];
//        _div2.layer.borderWidth = 1.0;
//        _div2.layer.borderColor = [UIColor redColor].CGColor;
    }
    return _div2;
}
- (MineHeaderBottomTitleCountView *)jiFenView {
    if (!_jiFenView) {
        
        NSString *vjfStr = NSLocalizedString(My_jiFen, @"我的V积分");
        _jiFenView = [[MineHeaderBottomTitleCountView alloc] initWithFrame:CGRectMake(0, 0, 100, 70) title:vjfStr count:@"0.0"];
    }
    return _jiFenView;
}
- (MineHeaderBottomTitleCountView *)kaQuanView {
    if (!_kaQuanView) {
        NSString *wdkqStr = NSLocalizedString(MyKaQuan, @"我的卡券");
        _kaQuanView = [[MineHeaderBottomTitleCountView alloc] initWithFrame:CGRectMake(0, 0, 100, 70) title:wdkqStr count:@"0"];
    }
    return _kaQuanView;
}

//- (MineHeaderBottomTitleCountView *)shouCangView {
//    if (!_shouCangView) {
//        NSString *collectionStr = NSLocalizedString(My_collection, @"我的收藏");
//
//        _shouCangView = [[MineHeaderBottomTitleCountView alloc] initWithFrame:CGRectMake(0, 0, 100, 70) title:collectionStr count:@"110"];
//    }
//    return _shouCangView;
//}

- (UIView *)lineView1 {
    if (!_lineView1) {
        _lineView1 = [UIView new];
        _lineView1.backgroundColor = kMainLightGrayColor;
    }
    return _lineView1;
}

//- (UIView *)lineView2 {
//    if (!_lineView2) {
//        _lineView2 = [UIView new];
//        _lineView2.backgroundColor = [UIColor lightGrayColor];
//    }
//    return _lineView2;
//}

//!!!!:对应 三个透明的按钮
- (UIButton *)jiFenBtn {
    if (!_jiFenBtn) {
        _jiFenBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_jiFenBtn addTarget:self action:@selector(clickJiFenAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _jiFenBtn;
}
- (UIButton *)kaQuanBtn {
    if (!_kaQuanBtn) {
        _kaQuanBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_kaQuanBtn addTarget:self action:@selector(clickKaQuanAction) forControlEvents:(UIControlEventTouchUpInside)];

    }
    return _kaQuanBtn;
}
//- (UIButton *)shouCangBtn {
//    if (!_shouCangBtn) {
//        _shouCangBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
//        [_shouCangBtn addTarget:self action:@selector(clickShouCangAction) forControlEvents:(UIControlEventTouchUpInside)];
//    }
//    return _shouCangBtn;
//}

- (UIButton *)createBtnAction:(SEL)action {
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [btn addTarget:self action:@selector(action) forControlEvents:(UIControlEventTouchUpInside)];

    return btn;
}



#pragma mark - 三 点击事件
- (void)clickSettingBtnAction:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(didClickSettingAction)]) {
        [self.delegate didClickSettingAction];
    }
}

- (void)clickMsgBtnAction:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(didClickMsgAction)]) {
        [self.delegate didClickMsgAction];
    }
}

- (void)clickJiFenAction {
    NSLog(@"积分");
    if ([self.delegate respondsToSelector:@selector(didClickMyJiFenAction)]) {
        [self.delegate didClickMyJiFenAction];
    }
}
- (void)clickKaQuanAction{
    NSLog(@"卡券");
    if ([self.delegate respondsToSelector:@selector(didClickMyKaQuanAction)]) {
        [self.delegate didClickMyKaQuanAction];
    }
}

- (void)clickTapGes {
    if ([self.delegate respondsToSelector:@selector(didClickAvatorAction)]) {
        [self.delegate didClickAvatorAction];
    }
}
//- (void)clickShouCangAction{
//    NSLog(@"收藏");
//    if ([self.delegate respondsToSelector:@selector(didClickMyShouCangAction)]) {
//        [self.delegate didClickMyShouCangAction];
//    }
//}


#pragma mark - 四 重新赋值
- (void)setMyModel:(MineModel *)myModel {
    _myModel = myModel;
    
    NSURL *url = [NSURL URLWithString:myModel.head];
    
    [self.iconIv sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"默认头像"]];
    //显示昵称
    self.phoneLb.text = myModel.nickname;
    //积分
    
    
    if ([myModel.bonus isEqualToNumber:@(0)]) {
        self.jiFenView.cntLb.text = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%.1f",[myModel.bonus floatValue]]];
    }else {
        self.jiFenView.cntLb.text = [NSString stringWithFormat:@"%@",myModel.bonus];
    }
    
    //卡券
    self.kaQuanView.cntLb.text = [NSString stringWithFormat:@"%@",myModel.coupon];
}

//显示隐藏小圆点
- (void)showRedDot {
    self.redDotView.hidden = NO;
}

- (void)hideRedDot {
    self.redDotView.hidden = YES;
}
@end
